import 'package:built_collection/built_collection.dart';
import 'package:equatable/equatable.dart';
import 'package:mobile_app.core/core/domain/entities/learning_content/collection_list_item/collection_list_item.dart';
import 'package:mobile_app.core/core/domain/entities/learning_content/lexical_item/lexical_item.dart';
import 'package:mobile_app.home.menu.settings/image_settings_page/domain/helpers/get_all_image_download_task_id/helper.dart';
import 'package:mobile_app.home.menu.settings/image_settings_page/domain/helpers/get_collection_ids/helper.dart';
import 'package:mobile_app.home.menu.settings/image_settings_page/domain/helpers/get_image_downloading_collection_ids/helper.dart';

part 'result.dart';

abstract class HandleDownloadTaskIdOuterUpdate {
  DownloadTaskIdOuterUpdateHandlingResult call({
    required BuiltList<CollectionListItem> collectionListItems,
    required BuiltMap<String, LexicalItem> lexicalItemMap,
    required BuiltSet<String> downloadTaskIds,
  });
}

class HandleDownloadTaskIdOuterUpdateImpl implements HandleDownloadTaskIdOuterUpdate {
  const HandleDownloadTaskIdOuterUpdateImpl({
    required GetAllImageDownloadTaskId getAllImageDownloadTaskId,
    required GetCollectionIds getCollectionIds,
    required GetImageDownloadingCollectionIds getImageDownloadingCollectionIds,
  })  : _getAllImageDownloadTaskId = getAllImageDownloadTaskId,
        _getCollectionIds = getCollectionIds,
        _getImageDownloadingCollectionIds = getImageDownloadingCollectionIds;

  final GetAllImageDownloadTaskId _getAllImageDownloadTaskId;
  final GetCollectionIds _getCollectionIds;
  final GetImageDownloadingCollectionIds _getImageDownloadingCollectionIds;

  @override
  DownloadTaskIdOuterUpdateHandlingResult call({
    required BuiltList<CollectionListItem> collectionListItems,
    required BuiltMap<String, LexicalItem> lexicalItemMap,
    required BuiltSet<String> downloadTaskIds,
  }) {
    final collectionIds = _getCollectionIds(
      collectionListItems: collectionListItems,
    );

    final imageDownloadingCollectionIds = _getImageDownloadingCollectionIds(
      collectionIds: collectionIds,
      downloadTaskIds: downloadTaskIds,
    );

    final allImageDownloadTaskId = _getAllImageDownloadTaskId();

    final allImagesDownloading = downloadTaskIds.contains(allImageDownloadTaskId);

    final result = DownloadTaskIdOuterUpdateHandlingResult(
      imageDownloadingCollectionIds: imageDownloadingCollectionIds,
      allImagesDownloading: allImagesDownloading,
    );

    return result;
  }
}
